package com.action.ui.activity;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.action.childbase.ChildBaseActivity;
import com.action.ui.view.PasswordView;
import com.action.utils.SoftKeyBoardListener;
import com.base.app_core.service.pressenter.NullPressenter;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.XYBAUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.xinglai.yun.R;
import com.tencent.map.lib.util.StringUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class SetPacketPasswordActivity extends ChildBaseActivity<NullPressenter> implements SoftKeyBoardListener.OnSoftKeyBoardChangeListener, PasswordView.PasswordListener {

    public static final String PASSWORD = "password";

    @BindView(R.id.pswView)
    PasswordView gridPasswordView;

    @BindView(R.id.ll_container)
    LinearLayout llContainer;

    @BindView(R.id.container)
    FrameLayout frameLayout;

    @BindView(R.id.cancel)
    TextView cancel;

    private SoftKeyBoardListener softKeyBoardListener;
    private String password;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_packet_password;
    }

    @Override
    protected void initView() {
        super.initView();
        password = getIntent().getStringExtra(AppConstants.EXTRA.PASSWORD);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, XYBAUtils.getWindowHeight() - BarUtils.getStatusBarHeight());
        ViewGroup.LayoutParams layoutParams = llContainer.getLayoutParams();
        layoutParams.width = (int) (XYBAUtils.getScreenWidth() * 0.9);
        softKeyBoardListener = new SoftKeyBoardListener(this);
        softKeyBoardListener.setListener(this, this);
        gridPasswordView.setPasswordListener(this);
        if (isEdit()) {
            gridPasswordView.setPassword(password);
            cancel.setText("不加密");
        }
    }

    private boolean isEdit() {
        return !StringUtils.isEmpty(password);
    }

    @OnClick({R.id.cancel, R.id.ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                if (isEdit()) {
                    gridPasswordView.clearPassword();
                    back("");
                }
                finiBack();
                break;
            case R.id.ok:
                if (isEdit()) {
                    finiBack();
                } else {
                    String psw = gridPasswordView.getPassword();
                    if (StringUtil.isEmpty(psw) || psw.length() == 4) {
                        showToast("请输入4位数密码");
                        return;
                    }
                    back(psw);
                }
                break;
        }
    }

    private void finiBack() {
        InputMethodManager inputManager = gridPasswordView.inputManager;
        gridPasswordView.clearFocus();
        if (inputManager != null) {
            inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
        finish();
    }

    private void back(final String psw) {
        Intent intent = getIntent();
        intent.putExtra(PASSWORD, psw);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void keyBoardShow(int height) {
        frameLayout.scrollTo(0, height / 2);
    }

    private int shouTranY(int height) {
        return -height + (XYBAUtils.getWindowHeight() - llContainer.getHeight()) / 2;
    }

    @Override
    public void keyBoardHide(int height) {
        frameLayout.scrollTo(0, 0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (softKeyBoardListener != null) {
            softKeyBoardListener.ondestory();
        }
        fixInputMethodManagerLeak(this);
    }


    @Override
    public void passwordChange(String changeText) {
    }

    @Override
    public void passwordComplete() {
        back(gridPasswordView.getPassword());
        KeyboardUtils.hideSoftInput(gridPasswordView);
    }

    @Override
    public void keyEnterPress(String password, boolean isComplete) {

    }

}
